﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.IO;
using System.Web;
using System.Web.Mvc;
using BenzInfo.Hockey.Grenadiers.Models;
using BenzInfo.Hockey.Grenadiers.Utils;

namespace BenzInfo.Hockey.Grenadiers.Controllers
{ 
    public class NewsFilesController : BaseController
    {

        private GrenadiersContext db = new GrenadiersContext();

        private const string BASE_PATH = "~/Content/newsfiles";

        //
        // GET: /NewsFiles/

        public ViewResult Index()
        {
            ViewBag.Years = db.NewsFiles.Select(nf => nf.Year).Distinct().OrderByDescending(nf => nf).ToList();
            return View(db.NewsFiles.OrderByDescending(nf => nf.Year).OrderByDescending(nf => nf.TitleEN).ToList());
        }

        //
        // GET: /NewsFiles/Details/5

        public ViewResult Details(int id)
        {
            NewsFile newsfile = db.NewsFiles.Find(id);
            return View(newsfile);
        }

        public ActionResult Add()
        {
            return PartialView();
        }

        [HttpPost]
        public ActionResult Add(NewsFile newsFile, HttpPostedFileBase inputFile)
        {
            if (ModelState.IsValid)
            {
                if (inputFile != null)
                {
                    string ext = inputFile.FileName.Split('.')[inputFile.FileName.Split('.').Length - 1];
                    string filename = string.Format("{0:000}.{1}", DateTime.Now.Ticks, ext);
                    string filepath = string.Format("{0}/{1}", Server.MapPath(BASE_PATH), filename);

                    newsFile.FileName = filename;
                    newsFile.PublicationDate = DateTime.Now;

                    using (FileStream fs = new FileStream(filepath, FileMode.Create))
                    {
                        StreamHelper.ReadWriteStream(inputFile.InputStream, fs);
                    }
                }

                db.NewsFiles.Add(newsFile);
                db.SaveChanges();

                return RedirectToAction("Index");
            }

            return PartialView(newsFile);
        }
        
        //
        // GET: /NewsFiles/Edit/5
 
        public ActionResult Edit(int id)
        {
            NewsFile newsfile = db.NewsFiles.Find(id);
            return PartialView(newsfile);
        }

        //
        // POST: /NewsFiles/Edit/5

        [HttpPost]
        public ActionResult Edit(NewsFile newsfile)
        {
            if (ModelState.IsValid)
            {
                db.Entry(newsfile).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(newsfile);
        }

        //
        // GET: /NewsFiles/Delete/5
 
        public ActionResult Delete(int id)
        {
            NewsFile newsfile = db.NewsFiles.Find(id);
            return View(newsfile);
        }

        //
        // POST: /NewsFiles/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            NewsFile newsfile = db.NewsFiles.Find(id);
            db.NewsFiles.Remove(newsfile);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}